package top.continew.admin.controller.qywx;

import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.rpc.wecom.api.RuleDataServiceApi;
import cn.shengchao.examstar.rpc.wecom.req.RuleDataPageQuery;
import cn.shengchao.examstar.rpc.wecom.resp.RuleDataVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.req.wecom.RuleDataChangeStateRto;
import top.continew.admin.req.wecom.RuleDataSaveRto;
import top.continew.starter.web.model.R;

/**
 * @program: examstar-manger-api
 * @description: 规则分配客服业务
 * @author: Xiong weiqiang
 * @create: 2025-04-12 13:46
 */
@Tag(name = "企微-客服分配规则")
@RestController
@RequestMapping("/qywx/csRule")
public class RuleAssignController {
    @DubboReference
    private RuleDataServiceApi ruleDataServiceApi;
    /**
     * 客服分配规则列表
     * @param pageQuery
     * @return
     */
    @Operation(summary = "客服分配规则列表", description = "客服分配规则列表")
    @GetMapping("/pageQuery")
    PageResult<RuleDataVo> pageQuery(RuleDataPageQuery pageQuery) {
        PageResult<RuleDataVo> pageResult = ruleDataServiceApi.getRuleDataListForPage(pageQuery);
        return pageResult;
    }

    /**
     * 客服分配规则保存
     * @param ruleDataSaveRto
     * @return
     */
    @Operation(summary = "客服分配规则保存", description = "客服分配规则保存")
    @PostMapping("/save")
    R<String> save(@Validated @RequestBody RuleDataSaveRto ruleDataSaveRto) {
        if (ruleDataSaveRto.getId()!=null&&ruleDataSaveRto.getId()==0L){
            return R.fail(R.fail().getCode(), "系统兜底规则不可编辑修改！");
        }
        ruleDataServiceApi.saveRuleData(ruleDataSaveRto.toRuleDataSaveDto());
        return R.ok("客服分配规则保存成功");
    }
    /**
     * 客服分配规则复制
     * @param ruleId
     * @return
     */
    @Operation(summary = "客服分配规则复制", description = "客服分配规则复制")
    @PostMapping("/copySave/{ruleId}")
    R<String> copySave(@PathVariable Long ruleId) {
        ruleDataServiceApi.copyRuleData(ruleId);
        return R.ok("客服分配规则保存成功");
    }
    /**
     * 客服分配规则改状态
     * @param stateRto
     * @return
     */
    @Operation(summary = "客服分配规则改状态", description = "客服分配规则改状态")
    @PostMapping("/changeStatus")
    R<String> changeStatus(@Validated @RequestBody RuleDataChangeStateRto stateRto) {
        if (stateRto.getRuleId()!=null&&stateRto.getRuleId()==0L){
            return R.fail(R.fail().getCode(), "系统兜底规则不能修改");
        }
        ruleDataServiceApi.changeRuleDataState(stateRto.getRuleId(), stateRto.getRuleState());
        return R.ok("更改状态成功");
    }

    /**
     * 删除客服分配规则
     *
     * @param ruleId
     * @return
     */
    @Operation(summary = "删除客服分配规则", description = "删除客服分配规则")
    @PostMapping("/delete/{ruleId}")
    R delete(@PathVariable Long ruleId) {
        if (ruleId!=null&&ruleId==0L){
            return R.fail(R.fail().getCode(), "系统兜底规则不能删除");
        }
        try{
            ruleDataServiceApi.deleteRuleData(ruleId);
        }
        catch (Exception e){
            return R.fail(R.fail().getCode(), "删除失败");
        }
        return R.ok("删除成功");
    }

}